﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace RepairTracker
{
    public partial class report : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                btnResetDate_Click(null, null);

                lstRepairFault.Items.Clear();
                lstRepairFault.Items.Add(new ListItem("(所有故障)", "0"));

                lstOwner.Items.Add(new ListItem("(所有网点)", "0"));
                RenderUI();
            }
            else
            {

            }
        }



        private void RenderUI() 
        {
            List<District> districts = Entity.GetAllDistricts();
            //List<Owner> owners = Entity.GetOwnersByDistrictId(1);
            List<Device> devices = Entity.GetAllDevices();
            //List<Fault> faults = Entity.GetAllFaults();


            lstDistrict.Items.Clear();
            lstDistrict.Items.Add(new ListItem("(全部区县)","0"));
            foreach (var dis in districts)
            {
                lstDistrict.Items.Add(new ListItem(dis.DistrictName,dis.Id.ToString()));
            }
            lstDevice.Items.Clear();
            lstDevice.Items.Add(new ListItem("(全部设备)", "0"));
            foreach (var dev in devices)
            {
                lstDevice.Items.Add(new ListItem(dev.DeviceName,dev.Id.ToString()));
            }

        }

        protected void btnResetDate_Click(object sender, EventArgs e)
        {
            caleInStart.SelectedDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            caleInEnd.SelectedDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));

            caleOutStart.SelectedDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            caleOutEnd.SelectedDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));

            //txtDateStart.Text = "";
            //txtDateEnd.Text = "";
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            ExportToExcel();
        }

        //Export to Excel from a GridView
        protected void ExportToExcel()
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls");
            Response.Charset = "";
            this.EnableViewState = false;

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

            GridView1.RenderControl(htw);

            Response.Write(sw.ToString());
            Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            //should be reserved
        }

        protected void lstDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Owner> owners = Entity.GetOwnersByDistrictId(int.Parse(lstDistrict.SelectedValue));
            lstOwner.Items.Clear();
            lstOwner.Items.Add(new ListItem("(所有网点)","0"));
            foreach (var owner in owners)
            {
                lstOwner.Items.Add(new ListItem(owner.DisplayName,owner.Id.ToString()));
            }

        }

        protected void lstDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Fault> faults = Entity.GetFaultsByDevice(int.Parse(lstDevice.SelectedValue));
            lstRepairFault.Items.Clear();
            lstRepairFault.Items.Add(new ListItem("(所有故障)", "0"));

            foreach (var fut in faults)
            {
                lstRepairFault.Items.Add(new ListItem(fut.FaultName, fut.Id.ToString()));
            }
        }

        protected void caleInStart_SelectionChanged(object sender, EventArgs e)
        {
            if (caleInEnd.SelectedDate.Subtract(caleInStart.SelectedDate).Days < 0)
            {
                caleInStart.SelectedDate = caleInEnd.SelectedDate;
            }

            //、//txtDateStart.Text = caleStart.SelectedDate.ToLongDateString();

        }

        protected void caleInEnd_SelectionChanged(object sender, EventArgs e)
        {
            if (caleInEnd.SelectedDate.Subtract(caleInStart.SelectedDate).Days < 0)
            {
                caleInEnd.SelectedDate = caleInStart.SelectedDate;
            }
            //、、txtDateEnd.Text = caleEnd.SelectedDate.ToLongDateString();

        }

        protected void caleOutStart_SelectionChanged(object sender, EventArgs e)
        {
            if (caleOutEnd.SelectedDate.Subtract(caleOutStart.SelectedDate).Days < 0)
            {
                caleOutStart.SelectedDate = caleOutEnd.SelectedDate;
            }

        }

        protected void caleOutEnd_SelectionChanged(object sender, EventArgs e)
        {
            if (caleOutEnd.SelectedDate.Subtract(caleOutStart.SelectedDate).Days < 0)
            {
                caleOutEnd.SelectedDate = caleOutStart.SelectedDate;
            }
        }

        protected void lstOwner_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void lstRepairFault_SelectedIndexChanged(object sender, EventArgs e)
        {

        }






    }
}